/***********************************************************************************
**  
* @copyright (c) 2010-2019,  Technology Co., LTD. All Right Reserved.
*
************************************************************************************/
/**
* @file	    duye_file_client.h
* @version     
* @brief      
* @author
* @date	    2013-04-05
* @note 
*
* 1. 2013-04-05 Created this file
*/

#pragma once

#include <string>
#include <duye_type.h>
#include <duye_tcp_client.h>

namespace duye {

class FileStat {
public:
    FileStat() : size(0) {}

    std::string path;
    uint32 size;
    std::string md5sum;
};

/**
 * @brief network file client
 */
class FileClient
{
public:
    FileClient();
    FileClient(const std::string& server_ip, const uint16 server_port);
    ~FileClient(); 

    /**
     * @brief file server is enabled
     * @param [in] server_ip : server ip
     * @param [in] server_port : server port
     * @return : true/false
     */
    bool test(const std::string& server_ip, const uint16 server_port);

    /**
     * @brief upload file
     * @param [in] server_ip : server ip
     * @param [in] server_port : server port
     * @param [in] file_path : file path
     * @param [out] error : error desc
     * @return : true/false
     */
    bool upload(const FileStat& local_file_stat, 
        const std::string& net_file_path);

    bool upload(const std::string& server_ip, 
        const uint16 server_port, 
        const FileStat& local_file_stat, 
        const std::string& net_file_path);
    
    /**
     * @brief upload file
     * @param [in] server_ip : server ip
     * @param [in] server_port : server port     
     * @param [in] net_file_path : net file path
     * @param [in] local_file_path : local file path
     * @param [out] error : error desc
     * @return : true/false
     */
    bool download(const FileStat& net_file_stat, 
        const std::string& local_file_path);

    bool download(const std::string& server_ip, 
        const uint16 server_port, 
        const FileStat& net_file_stat, 
        const std::string& local_file_path);

private:
    bool connect();
    bool reconnect();
    bool disconnect();    

    bool up(const FileStat& local_file_stat, 
        const std::string& net_file_path);

    bool down(const FileStat& net_file_stat,
        const std::string& local_file_path);

private:
    TcpClient       m_tcp_client;
    std::string     m_server_ip;
    uint16          m_server_port;
    bool            m_is_connected;
};
}
